//
//  LGSportOrderListController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/6.
//  运动订单 -- 购买订单

#import "LGSportOrderTableController.h"
#import "LGSportOrderDetailController.h"
#import "LGSportOrderSearchController.h"
#import "LGSportOrderTableViewCell.h"
#import "LGSportScreenOrderView.h"
#import "LGSportOrderDetailController.h"
#import "LGOrderDetailController.h"
@interface LGSportOrderTableController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *searchBtn;
/**   */
@property (nonatomic , strong)UIButton *screenBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , strong)NSArray *statusArray;
/**   */
@property (nonatomic , copy)NSString *status;
/**   */
@property (nonatomic , assign)NSInteger pageNo;

@end

@implementation LGSportOrderTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.status = @"";
    self.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.searchBtn];
    [self.view addSubview:self.screenBtn];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
    [self requestOptions];
}
-(void)requestData{
    self.pageNo = 1;
    self.dataArray = [NSMutableArray array];
    [self requestMoreData];
}

-(void)requestMoreData{
    [RequestUtil withPOST:@"/doctor/mall/sportList" parameters:@{@"pageNo":@(self.pageNo),@"pageSize":@"10",@"state":self.status,@"keyword":@""} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(code == 200){
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo ++;
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无订单" offset:0];
        }
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
        }
    }];
}

//请求筛选状态
-(void)requestOptions{
    [RequestUtil withPOST:@"/api/common.dict/sportOrderStateOptions" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            NSMutableArray *array = [NSMutableArray arrayWithArray:returnData];
            [array insertObject:@{@"value":@"",@"title":@"全部"} atIndex:0];
            self.statusArray = array;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)searchBtnAction{
    LGSportOrderSearchController *controller = [[LGSportOrderSearchController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)screenBtnAction{
    if (self.statusArray.count == 0){
        [self requestOptions];
    }else{
        LGSportScreenOrderView *screenView = [[LGSportScreenOrderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        screenView.status = self.status;
        screenView.statusArray = self.statusArray;
        [self.view.window addSubview:screenView];
        __weak typeof(self) weakSelf = self;
        screenView.screenOrderResult = ^(NSString * _Nonnull status) {
            weakSelf.status = status;
            [weakSelf requestData];
        };
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSportOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.orderDic = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGSportOrderDetailController *controller = [[LGSportOrderDetailController alloc]init];
    controller.recordid = LGNSString(tempDic[@"record_id"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(UIButton *)searchBtn{
    if(!_searchBtn){
        _searchBtn = [UIButton buttonWithTitle:@"输入患者姓名或订单编号查询" titleFont:LGFont(14) textColor:@"#999999" imageName:@"我的订单-搜索"];
        _searchBtn.frame = CGRectMake(viewPix(16), viewPix(15), Screen_W-viewPix(105), viewPix(34));
        _searchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _searchBtn.layer.borderColor = [UIColor colorWithString:@"#F0F0F0"].CGColor;
        _searchBtn.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _searchBtn.layer.borderWidth = 1;
        _searchBtn.cornerRidus = viewPix(17);
        _searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        _searchBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(15), 0, 0);
        [_searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

-(UIButton *)screenBtn{
    if(!_screenBtn){
        _screenBtn = [UIButton buttonWithTitle:@"筛选" titleFont:LGFont(15) textColor:@"#666666" imageName:@"我的订单-筛选"];
        _screenBtn.frame = CGRectMake(Screen_W-viewPix(85), viewPix(16), viewPix(83), viewPix(32));
        [_screenBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
        [_screenBtn addTarget:self action:@selector(screenBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _screenBtn;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(56), Screen_W, Screen_H-topBarHeight-viewPix(56)-viewPix(44)) style:UITableViewStylePlain
        ];
        [_tableView registerClass:[LGSportOrderTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = viewPix(148);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}





@end
